package src;
import java.util.*;
/**
 *
 * @author cyhaberl
 */
public class PlanningPeriode {

	private int formation;
	private int promotion;
	public List<PlanningSemaine> tabPeriode;
	private GregorianCalendar dateDeb;
	private GregorianCalendar dateFin;

	public PlanningPeriode(int formation, int promotion, int jDeb, int mDeb, int aDeb, int jFin, int mFin, int aFin) {

		this.formation = formation;
		this.promotion = promotion;
		this.dateDeb = new GregorianCalendar();
		this.dateFin = new GregorianCalendar();
		this.dateDeb.set(aDeb,mDeb,jDeb);
		this.dateFin.set(aFin,mFin,jFin);
		this.remplir();
	}

	public final void remplir(){
		long MILLISECONDS_PER_WEEK = 1000*60*60*24*7;
		GregorianCalendar semSuiv = new GregorianCalendar();
		PlanningSemaine semaine;
		double nbSem = 0;
		this.tabPeriode = new ArrayList();
		try
		{
			nbSem = Math.ceil((this.dateFin.getTimeInMillis() - this.dateDeb.getTimeInMillis())/MILLISECONDS_PER_WEEK);
			semSuiv.setTimeInMillis(this.dateDeb.getTimeInMillis());
			for(int i = 0; i < nbSem; i++)
			{
				semaine = new PlanningSemaine(semSuiv.get(Calendar.DATE),semSuiv.get(Calendar.MONTH),semSuiv.get(Calendar.YEAR),this.formation,this.promotion);
				//this.tabPeriode.add(new PlanningSemaine(01, 01, 2011, "M1", 1));
				semSuiv.setTimeInMillis(semSuiv.getTimeInMillis()+MILLISECONDS_PER_WEEK);
			}
		}
		catch(IllegalArgumentException iAE)
		{
			System.out.println("Erreur dans la saisie des dates !");
		}
	}
	
	public String afficherPeriode()
	{
		StringBuilder planning = new StringBuilder();

		for(PlanningSemaine semaine : this.tabPeriode)
		{
			planning.append(semaine.afficherSemaine());
			planning.append("<br><br>");
		}
		return planning.toString();
	}
}
